package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTask;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskRepository;

/*** 老师---备授课--授课报告-作业 **/

@CrossOrigin
@RestController
public class LectureReportClassHomeWorkController extends YxyApiBaseController {

    @Autowired
    private HomeworkTaskRepository homeworkTaskRepository;

    @Autowired
    private HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @Autowired
    private HomeworkTaskReportRepository homeworkTaskReportRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/testreport/homework/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String lectureId = reqModel.lectureId;
        String classId = reqModel.classId;
        String schoolId = getProfile(request, response).getSchoolId();
        Assert.notNull(StringUtils.defaultIfBlank(lectureId, null), "授课包ID为空");
        // 去掉lectureId 2017-11-15
        List<HomeworkTask> hList = homeworkTaskRepository.findBySchoolIdAndSource(schoolId, 1);
        List<RespModel> respModels = new ArrayList<RespModel>();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Integer status = 0;
        for (HomeworkTask homeworkTask : hList) {
            RespModel model = new RespModel();
            model.lectureId = lectureId;
            model.quesGroupName = homeworkTask.getTitle();
            List<HomeworkTaskQues> list = homeworkTaskQuesRepository.findByTaskId(homeworkTask.getId());
            // 文档类只有一条数据
            for (HomeworkTaskQues homeworkTaskQues : list) {
                if (null != homeworkTaskQues && null != homeworkTaskQues.getQuesArticle()) {
                    model.docNum = homeworkTaskQues.getQuesArticle().size();
                }
            }
            model.quesNum = list.size();
            model.taskId = homeworkTask.getId();
            model.taskType = homeworkTask.getTaskType();
            model.lectureType = homeworkTask.getLectureType();
            model.createdDate = sdf.format(homeworkTask.getCreatedDate());
            Date predictDate = homeworkTask.getPredictDate();
            Integer staInteger = 0;
            List<HomeworkTaskReport> hTaskReportsList = homeworkTaskReportRepository
                    .findBySchoolIdAndClassIdAndLectureIdAndTaskId(schoolId, classId, lectureId, homeworkTask.getId());
            if (null != hTaskReportsList) {
                for (HomeworkTaskReport homeworkTaskReport : hTaskReportsList) {
                    if (homeworkTaskReport.getStatusOfFinished() != 1
                            || homeworkTaskReport.getStatusOfFinished() != 2) {
                        staInteger = 1;
                    }
                }
            }
            if (date.getTime() < predictDate.getTime()) {
                status = 0;
            } else if (date.getTime() > predictDate.getTime() && staInteger == 1) {
                status = 1;
            } else {
                status = 2;
            }
            model.status = status;
            respModels.add(model);
        }
        return respModels;

    }

    protected static class ReqModel extends YxyApiReqModel {
        // 授课包id
        public String lectureId;
        // 班级ID
        public String classId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 授课包id
        public String lectureId;
        // 题组logo
        public String quesGroupImage;
        // 题组名称
        public String quesGroupName;
        // 试题数量
        public Integer quesNum;
        // 任务ID
        public String taskId;
        // 文档数量
        public Integer docNum;
        // 任务类型
        public String taskType;
        // 课后、预习0--预习作业 1--课后作业
        public Integer lectureType;
        // 创建时间
        public String createdDate;
        // 完成状态 0 未完成 1 未批阅 2 已批阅
        public Integer status;
    }
}
